//
//  LBWTabBar.swift
//  weibo
//
//  Created by Limonwei on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

//tabbar基类

import UIKit

class LBWTabBar: UITabBar {
    
    //闭包
    var composeButtonClosure: (()->())?
    
    //初始化frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //设置UI
    func setupUI(){
        
        backgroundImage = UIImage(named: "tabbar_background")
        
        addSubview(composeButton)
        
    }

    
    //撰写按钮
  
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        //按钮的点击事件
        button.addTarget(self, action: "ButtonClickClosure", forControlEvents: UIControlEvents.TouchUpInside)
        
        //按钮的背景图片 两种状态
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        
        //设置加号按钮 添加上来
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        
        button.sizeToFit()
        
        return button
    }()
    
    //MARK: - 按钮监听
    @objc private func ButtonClickClosure(){
        printLog("按钮点击")
        
        composeButtonClosure?()
    }
    
    
    //设置加号按钮的位置  layoutSubView 自动布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //加号按钮居中
        composeButton.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
        
        //宽度 五个按钮的
        let childW = frame.width / 5
        
        //按钮下标
        var index = 0
        
        //遍历子视图
        for childView in subviews {
            
            //判断类型
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                let x = CGFloat(index) * childW
                
                childView.frame = CGRect(x: x, y: 0, width: childW, height: frame.height)
               
                
                //递增
                index++
                
                if index == 2 {
                    
                    index++
                }
            }
        }
    }
}















